<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>用户登录</h2>
      </div>
      
      <!-- 登录提示，当用户被重定向到登录页面时显示 -->
      <el-alert
        v-if="showLoginTip"
        title="请先登录"
        type="info"
        description="您尝试访问的页面需要登录后才能查看"
        show-icon
        :closable="true"
        style="margin-bottom: 20px;"
      />
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="User"
            placeholder="用户名"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="Lock"
            placeholder="密码"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <div class="remember-container">
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
            <router-link to="/forgot-password" class="forgot-link">忘记密码?</router-link>
          </div>
        </el-form-item>
        
        <el-button
          type="primary"
          class="login-button"
          :loading="loading"
          @click="handleLogin"
        >
          登录
        </el-button>
        
        <div class="register-link">
          还没有账号? <router-link to="/register">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, ref, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Login',
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    // 获取是否有重定向参数
    const redirectPath = computed(() => route.query.redirect || '/')
    const needLogin = computed(() => route.query.needLogin === 'true')
    const showLoginTip = ref(false)
    
    // 用户登录表单数据
    const loginForm = reactive({
      username: '',
      password: '',
      remember: false
    })
    
    // 表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    // 登录处理
    const handleLogin = () => {
      loginFormRef.value.validate(valid => {
        if (valid) {
          loading.value = true
          
          // 调用登录接口
          store.dispatch('user/login', {
            username: loginForm.username,
            password: loginForm.password,
            remember: loginForm.remember
          })
          .then(() => {
            ElMessage({
              message: '登录成功',
              type: 'success'
            })
            
            // 登录成功后进行跳转
            handleLoginSuccess()
          })
          .catch(error => {
            console.error('登录失败:', error)
            ElMessage({
              message: error.message || '用户名或密码错误',
              type: 'error',
              duration: 5000
            })
          })
          .finally(() => {
            loading.value = false
          })
        }
      })
    }
    
    // 在页面加载时检查是否需要显示登录提示
    onMounted(() => {
      showLoginTip.value = needLogin.value || false
    })
    
    // 登录成功后处理函数
    const handleLoginSuccess = () => {
      // 登录成功后，根据redirectPath进行跳转
      router.push(redirectPath.value)
    }
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      showLoginTip,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 200px);
  padding: 40px 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
}

.login-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.login-button {
  width: 100%;
  margin-top: 20px;
}

.remember-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-link {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
}

.register-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
}
</style> 